<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>SFML - Simple and Fast Multimedia Library</title>
        <meta http-equiv="Content-Type" content="text/html;"/>
        <meta charset="utf-8"/>
        <!--<link rel='stylesheet' type='text/css' href="https://fonts.googleapis.com/css?family=Ubuntu:400,700,400italic"/>-->
        <link rel="stylesheet" type="text/css" href="doxygen.css" title="default" media="screen,print" />
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="dynsections.js"></script>
        <link rel="stylesheet" type="text/css" href="search/search.css" />
        <link rel="stylesheet" type="text/css" href="searchOverrides.css" />
        <script type="text/javascript" src="search/searchdata.js"></script>
        <script type="text/javascript" src="search/search.js"></script>
        <script type="text/javascript">
        /* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
          $(document).ready(function() { init_search(); });
        /* @license-end */
        </script>
    </head>
    <body>
        <div id="banner-container">
            <div id="banner">
                <span id="sfml">SFML 2.6.1</span>
            </div>
        </div>
        <div id="content">
<!-- Generated by Doxygen 1.9.7 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li class="current"><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <span id="MSearchSelect"                onmouseover="return searchBox.OnSearchSelectShow()"                onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
          <input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="annotated.html"><span>Class&#160;List</span></a></li>
      <li><a href="classes.html"><span>Class&#160;Index</span></a></li>
      <li><a href="hierarchy.html"><span>Class&#160;Hierarchy</span></a></li>
      <li><a href="functions.html"><span>Class&#160;Members</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><b>sf</b></li><li class="navelem"><a class="el" href="classsf_1_1Lock.html">Lock</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="summary">
<a href="#pub-methods">Public Member Functions</a> &#124;
<a href="classsf_1_1Lock-members.html">List of all members</a>  </div>
  <div class="headertitle"><div class="title">sf::Lock Class Reference<div class="ingroups"><a class="el" href="group__system.html">System module</a></div></div></div>
</div><!--header-->
<div class="contents">

<p>Automatic wrapper for locking and unlocking mutexes.  
 <a href="classsf_1_1Lock.html#details">More...</a></p>

<p><code>#include &lt;<a class="el" href="Lock_8hpp_source.html">SFML/System/Lock.hpp</a>&gt;</code></p>
<div class="dynheader">
Inheritance diagram for sf::Lock:</div>
<div class="dyncontent">
 <div class="center">
  <img src="classsf_1_1Lock.png" usemap="#sf::Lock_map" alt=""/>
  <map id="sf::Lock_map" name="sf::Lock_map">
<area href="classsf_1_1NonCopyable.html" title="Utility class that makes any derived class non-copyable." alt="sf::NonCopyable" shape="rect" coords="0,0,105,24"/>
  </map>
</div></div>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="pub-methods" name="pub-methods"></a>
Public Member Functions</h2></td></tr>
<tr class="memitem:a1a4c5d7a15da61103d85c9aa7f118920"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsf_1_1Lock.html#a1a4c5d7a15da61103d85c9aa7f118920">Lock</a> (<a class="el" href="classsf_1_1Mutex.html">Mutex</a> &amp;mutex)</td></tr>
<tr class="memdesc:a1a4c5d7a15da61103d85c9aa7f118920"><td class="mdescLeft">&#160;</td><td class="mdescRight">Construct the lock with a target mutex.  <br /></td></tr>
<tr class="separator:a1a4c5d7a15da61103d85c9aa7f118920"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a8168b36323a18ccf5b6bc531d964aec5"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classsf_1_1Lock.html#a8168b36323a18ccf5b6bc531d964aec5">~Lock</a> ()</td></tr>
<tr class="memdesc:a8168b36323a18ccf5b6bc531d964aec5"><td class="mdescLeft">&#160;</td><td class="mdescRight">Destructor.  <br /></td></tr>
<tr class="separator:a8168b36323a18ccf5b6bc531d964aec5"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Automatic wrapper for locking and unlocking mutexes. </p>
<p><a class="el" href="classsf_1_1Lock.html" title="Automatic wrapper for locking and unlocking mutexes.">sf::Lock</a> is a RAII wrapper for <a class="el" href="classsf_1_1Mutex.html" title="Blocks concurrent access to shared resources from multiple threads.">sf::Mutex</a>.</p>
<p>By unlocking it in its destructor, it ensures that the mutex will always be released when the current scope (most likely a function) ends. This is even more important when an exception or an early return statement can interrupt the execution flow of the function.</p>
<p>For maximum robustness, <a class="el" href="classsf_1_1Lock.html" title="Automatic wrapper for locking and unlocking mutexes.">sf::Lock</a> should always be used to lock/unlock a mutex.</p>
<p>Usage example: </p><div class="fragment"><div class="line"><a class="code hl_class" href="classsf_1_1Mutex.html">sf::Mutex</a> mutex;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> function()</div>
<div class="line">{</div>
<div class="line">    <a class="code hl_class" href="classsf_1_1Lock.html">sf::Lock</a> lock(mutex); <span class="comment">// mutex is now locked</span></div>
<div class="line"> </div>
<div class="line">    functionThatMayThrowAnException(); <span class="comment">// mutex is unlocked if this function throws</span></div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">if</span> (someCondition)</div>
<div class="line">        <span class="keywordflow">return</span>; <span class="comment">// mutex is unlocked</span></div>
<div class="line"> </div>
<div class="line">} <span class="comment">// mutex is unlocked</span></div>
<div class="ttc" id="aclasssf_1_1Lock_html"><div class="ttname"><a href="classsf_1_1Lock.html">sf::Lock</a></div><div class="ttdoc">Automatic wrapper for locking and unlocking mutexes.</div><div class="ttdef"><b>Definition</b> <a href="Lock_8hpp_source.html#l00043">Lock.hpp:44</a></div></div>
<div class="ttc" id="aclasssf_1_1Mutex_html"><div class="ttname"><a href="classsf_1_1Mutex.html">sf::Mutex</a></div><div class="ttdoc">Blocks concurrent access to shared resources from multiple threads.</div><div class="ttdef"><b>Definition</b> <a href="Mutex_8hpp_source.html#l00047">Mutex.hpp:48</a></div></div>
</div><!-- fragment --><p>Because the mutex is not explicitly unlocked in the code, it may remain locked longer than needed. If the region of the code that needs to be protected by the mutex is not the entire function, a good practice is to create a smaller, inner scope so that the lock is limited to this part of the code.</p>
<div class="fragment"><div class="line"><a class="code hl_class" href="classsf_1_1Mutex.html">sf::Mutex</a> mutex;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> function()</div>
<div class="line">{</div>
<div class="line">    {</div>
<div class="line">      <a class="code hl_class" href="classsf_1_1Lock.html">sf::Lock</a> lock(mutex);</div>
<div class="line">      codeThatRequiresProtection();</div>
<div class="line"> </div>
<div class="line">    } <span class="comment">// mutex is unlocked here</span></div>
<div class="line"> </div>
<div class="line">    codeThatDoesntCareAboutTheMutex();</div>
<div class="line">}</div>
</div><!-- fragment --><p>Having a mutex locked longer than required is a bad practice which can lead to bad performances. Don't forget that when a mutex is locked, other threads may be waiting doing nothing until it is released.</p>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="classsf_1_1Mutex.html" title="Blocks concurrent access to shared resources from multiple threads.">sf::Mutex</a> </dd></dl>

<p class="definition">Definition at line <a class="el" href="Lock_8hpp_source.html#l00043">43</a> of file <a class="el" href="Lock_8hpp_source.html">Lock.hpp</a>.</p>
</div><h2 class="groupheader">Constructor &amp; Destructor Documentation</h2>
<a id="a1a4c5d7a15da61103d85c9aa7f118920" name="a1a4c5d7a15da61103d85c9aa7f118920"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a1a4c5d7a15da61103d85c9aa7f118920">&#9670;&#160;</a></span>Lock()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">sf::Lock::Lock </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="classsf_1_1Mutex.html">Mutex</a> &amp;&#160;</td>
          <td class="paramname"><em>mutex</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">explicit</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Construct the lock with a target mutex. </p>
<p>The mutex passed to <a class="el" href="classsf_1_1Lock.html" title="Automatic wrapper for locking and unlocking mutexes.">sf::Lock</a> is automatically locked.</p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">mutex</td><td><a class="el" href="classsf_1_1Mutex.html" title="Blocks concurrent access to shared resources from multiple threads.">Mutex</a> to lock </td></tr>
  </table>
  </dd>
</dl>

</div>
</div>
<a id="a8168b36323a18ccf5b6bc531d964aec5" name="a8168b36323a18ccf5b6bc531d964aec5"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a8168b36323a18ccf5b6bc531d964aec5">&#9670;&#160;</a></span>~Lock()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">sf::Lock::~Lock </td>
          <td>(</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Destructor. </p>
<p>The destructor of <a class="el" href="classsf_1_1Lock.html" title="Automatic wrapper for locking and unlocking mutexes.">sf::Lock</a> automatically unlocks its mutex. </p>

</div>
</div>
<hr/>The documentation for this class was generated from the following file:<ul>
<li><a class="el" href="Lock_8hpp_source.html">Lock.hpp</a></li>
</ul>
</div><!-- contents -->
        </div>
        <div id="footer-container">
            <div id="footer">
                SFML is licensed under the terms and conditions of the <a href="https://www.sfml-dev.org/license.php">zlib/png license</a>.<br>
                Copyright &copy; Laurent Gomila &nbsp;::&nbsp;
                Documentation generated by <a href="http://www.doxygen.org/" title="doxygen website">doxygen</a> &nbsp;::&nbsp;
            </div>
        </div>
    </body>
</html>
